<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>search properties</title>
<!-- 	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
	<link rel="stylesheet" href="stylesheet.css">	
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> -->
</head>
<body>
	
<?php
require_once 'header.php';
require 'connect.php';

$status = '';

if( isset($_POST['submit'])){

// print_r($_POST); /////displaying for debuging purposes only

	if( !empty($_POST['city'])){

		$city = $_POST['city'];

		$sql ="SELECT bedrooms, price, description, suburb, city FROM properties
		INNER JOIN addresses ON addresses.property_id = properties.id
		WHERE city = '".$city."'";

		// echo '$_POST[city]';/////displaying for debuging purposes only
		
		if( !empty($_POST['suburbs'])){

			// print_r($_POST['suburbs']);/////displaying for debuging purposes only

			$suburbs_names = "";

			foreach($_POST['suburbs'] as $value){
				if (empty($suburbs_names)){

					$suburbs_names = "'".$value."'"; 
				}
				else{ $suburbs_names = $suburbs_names.", '.$value.'"; }

        }
        $sql = "SELECT bedrooms, price, description, suburb, city, properties.id FROM properties 
					INNER JOIN addresses ON addresses.property_id = properties.id
					WHERE addresses.suburb IN (".$suburbs_names.")";
		}

		if(!empty($_POST['price'])){
			$price = $_POST['price'];
			$sql = $sql." AND price > ".$price;
		}
		if (!empty($_POST['bedrooms'])){
			$bedrooms = $_POST['bedrooms'];
			$sql = $sql." AND bedrooms > ".$bedrooms;
		}


// echo $sql;/////displaying for debuging purposes only

		$result = mysqli_query($conn, $sql);
		if (mysqli_num_rows($result) > 0){
			while($row = mysqli_fetch_assoc($result)){


				print_r($row);////displaying for debuging purposes only

				//getting photos urls or current property and adding them to array as well
				$sql = "SELECT photo_url FROM photos WHERE property_id = $row[id]";
				$result_photos = mysqli_query($conn, $sql);
				if (mysqli_num_rows($result_photos) > 0){
					$current_property_photos_urls = mysqli_fetch_assoc($result_photos);

					print_r($current_property_photos_urls);////displaying for debuging purposes only

				}
			}
		}

	} else {$status = "City has to be selected.";}
}

?>
<!-- This will take all city names from current DB and add them to 
 search list tags in html via PHP. After that, depending on selected city, related suburbs are going to be put from DB inside drop check list.
 So customer will only be able to choose from what data actualiy in DB.
 This prevents typos and unneccessery search requests. -->

<script type="text/javascript">
   $(document).ready(function(){
   	// City dependent ajax
   	$("#city").on("change",function(){
   	var city_selected = this.value;
    $.ajax({
		url: "suburb_list.php",
		type: "POST",
		cache:false,
		data:{ city_selected: city_selected },
  		success:function(result){
      	$("#suburb").html(result);
  		}
    });	
  });
});
</script>

<div id="search-wrapper">
	<div id="search-form" class='container'>
		<div class="row">
			<div class="col-md-8 offset-2">
				<div class="card mt-5">
					<div id="card-header">
						<h1 id="form-se-head">PROPERTY SEARCH FORM</h1>
					</div>
					<div class="card-body">
						<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
							<div class="form-row">
								<div class="form-group col">
									<select class="form-control" id="city" name="city">
										<option>SELECT CITY</option>
										<!-- adding all distinct cities from DB to search list -->
										<?php 
										$result = mysqli_query($conn,"SELECT DISTINCT city from addresses ORDER BY city ASC");
										if ($result->num_rows > 0){
											while($row = mysqli_fetch_array($result)){ 
										?>
										<option value="<?php echo $row['city'];?>"><?php echo $row['city'];?></option>
										<?php
											}
										} else {$query_error = 'some error getting city data';}
										?>
									</select>
								</div>

								<div class="col">
									<!-- this dropdown populates from first one with 
											help of AJAX script above divs and suburb_list.php -->
									<div class="dropdown input-group">
									<button class="btn btn-secondary dropdown-toggle btn-block" type="button" 
									data-toggle="dropdown">
									SELECT SUBURB
									</button>
									<div class="dropdown-menu">
									<button class="dropdown-item" type="button" id="suburb">
									<option>no suburb detected</option>
									</button>
									</div>
									</div>
								</div>	

							</div>
							<div class=" form-row">
								<div class=" col">
									<select class="form-control" name='price'>
										<option>Price</option>
										<option value="150000">$150K+</option>
										<option value="300000">$300K+</option>
										<option value="600000">$600K+</option>
										<option value="1000000">$1M+</option>
									</select>
								</div>
								<div class=" col">
									<select class="form-control" name='bedrooms'>
										<option>Bedrooms</option>
										<option value="1">1+</option>
										<option value="2">2+</option>
										<option value="3">3+</option>
									</select>
								</div>
							</div>

							<div class="form-group text-center">
									<input id="sub-btn-search" type="submit" name="submit" class="btn-max btn-size" value="Submit" id="submit_search">
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
</body>
</html>